package cn.com.cabinet.manager.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cabinet.web.entity.Admin;

import cn.com.cabinet.dao.AdminDao;
import cn.com.easy.utils.SecurityUtils;

@Controller
@RequestMapping("/manager/manager")
public class managerController {
	
	@Autowired
	private AdminDao adminDao;
	
	@RequestMapping("/index")
	public String index(ModelMap modelMap){
		
		List<Admin> admin = adminDao.findAll();
		modelMap.addAttribute("admin", admin);
		return "/util/addmanager/manager";
		
	}
	
	@RequestMapping("/addmanager")
	public String addmanager(){
		return "/util/addmanager/index";
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public int deleteAdmin(Long Id){
		Admin admin = adminDao.findOne(Id);
		adminDao.delete(admin);
		return 1;
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public int savaAdmin(String Acount,String Password){
		
		Admin ad = adminDao.getAdminByAdminAccount(Acount);
		if(ad == null){
			String password = SecurityUtils.SHA256(Password);
			Admin admin = new Admin();
			admin.setAccount(Acount);
			admin.setPassword(password);
			adminDao.save(admin);
			return 1;
		}else{
			return 0;
		}
	}

}
